package com.example.demo.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.EducationExperience;
import com.example.demo.entity.ExpectedPosition;
import com.example.demo.service.EducationExperienceService;
import com.example.demo.utils.DateUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 教育经历表(EducationExperience)表控制层
 *
 * @author makejava
 * @since 2023-12-19 08:33:15
 */
@RestController
@RequestMapping("educationExperience")
public class EducationExperienceController {
    /**
     * 服务对象
     */
    @Autowired
    private EducationExperienceService educationExperienceService;


    /**
     * 新增/编辑数据
     *
     * @param educationExperience 实体对象
     * @return 新增/编辑结果
     */
    @PostMapping("doAdd")
    public Result doAdd(@RequestBody EducationExperience educationExperience) {
        String timestampPattern = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z$";
        if (educationExperience.getStartTime()!=null){
            if (Pattern.matches(timestampPattern, educationExperience.getStartTime())) {
                String startConvert= DateUtil.convertDate(educationExperience.getStartTime());
                educationExperience.setStartTime(startConvert);
            }
        }
        if (educationExperience.getEndTime()!=null){
            if (Pattern.matches(timestampPattern, educationExperience.getEndTime())) {
                String endConvert= DateUtil.convertDate(educationExperience.getEndTime());
                educationExperience.setEndTime(endConvert);
            }
        }
        try {
            if (educationExperience.getId()==null){
                educationExperienceService.save(educationExperience);
                return Result.success("教育经历添加成功");
            } else {
                UpdateWrapper<EducationExperience> wrapper=new UpdateWrapper<>();
                wrapper.eq("id",educationExperience.getId());
                educationExperienceService.update(educationExperience,wrapper);
                return Result.success("教育经历编辑成功");
            }
        }catch (Exception e){
            return Result.error("参数错误");
        }
    }


    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("delete")
    public Result delete(Integer id) {
        try {
            QueryWrapper<EducationExperience> wrapper=new QueryWrapper<>();
            wrapper.eq("id",id);
            educationExperienceService.remove(wrapper);
            return Result.success("教育经历删除成功");
        }catch (Exception e){
            return Result.error("id不存在");
        }
    }
}

